package fw.qaz.asmx.reader.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;

import fw.qaz.asmx.reader.R;
import fw.qaz.asmx.reader.SettingCenter;
import fw.qaz.asmx.reader.dialog.Dialogs;

public class SettingsFragment extends Fragment
{
    SharedPreferences sp;//使用的SharedPreferences（用默认的，在 /data/data/包名/shared_prefs/preferences.xml）
    SharedPreferences.Editor edit;

    //要保存的控件
    private SeekBar seekBarSetFontSize;//设置字体大小的sekkbar
    private int seekBarSetFontSizeMin =12;//字体的最小值 //不需要这了个
    private TextView textViewFontSize;//显示字体大小的textview
    private Button btnSelectLanguage;//选择语言按钮
    private AlertDialog adialogSelectLanguage; //选择语言的dialog
    String[] languages;//语言的集合
    private int langId = 0;//语言id，默认中文0

    Dialogs mydialogs;//我的自定义dialogs
    private Button btnSelectFontColor;//选择字体颜色的按钮
    private Button btnSelectBgColor;//选择背景颜色的按钮
    private Button btnSaveSetting;//保存按钮

    public SettingsFragment()
    {
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        sp = this.getActivity().getSharedPreferences("preferences", Context.MODE_PRIVATE);
        edit = sp.edit();
        languages = getResources().getStringArray(R.array.languageOptions);
        mydialogs = new Dialogs(getActivity());
    }

    public static int getStatusBarHeight(Context context)
    {
        Class<?> c;
        Object obj;
        Field field;
        int x, statusBarHeight = 0;
        try
        {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_setting, container, false);

        //获取控件并获取值获取控件并获得各自的值
        seekBarSetFontSize = (SeekBar) v.findViewById(R.id.seekBarSetFontSize);   //调字体大小的
        seekBarSetFontSize.setProgress(  SettingCenter.getFontSizeMAO(getActivity()) - seekBarSetFontSizeMin);//不存在的话，默认12
        textViewFontSize = (TextView) v.findViewById(R.id.textViewFontSize);
        textViewFontSize.setText(  String.valueOf(SettingCenter.getFontSizeMAO(getActivity()))  );
        seekBarSetFontSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b)
            {
                textViewFontSize.setText(String.valueOf(i+seekBarSetFontSizeMin));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {

            }
        });

        btnSelectFontColor = (Button) v.findViewById(R.id.btnSelectFontColor);
        btnSelectFontColor.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                mydialogs.SelectColorAlertDialog("fontColor").show();
            }
        });


        btnSelectBgColor = (Button) v.findViewById(R.id.btnSelectBgColor);
        btnSelectBgColor.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                mydialogs.SelectColorAlertDialog("bgColor").show();
            }
        });


        btnSelectLanguage = (Button) v.findViewById(R.id.btnSelectLanguage);      //选择语言的
        btnSelectLanguage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getActivity(), R.string.choose_language_please, Toast.LENGTH_SHORT).show();
                adialogSelectLanguage.show();
            }
        });

        adialogSelectLanguage = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.choose_language).setIcon(R.mipmap.mao)
                .setSingleChoiceItems(languages, sp.getInt("lang_id", 0),
                                      new DialogInterface.OnClickListener()
                                      {
                                          public void onClick(DialogInterface dialog, int which)
                                          {
                                              edit.putInt("lang_id", which)
                                                  .commit();
                                              dialog.dismiss();
                                          }
                                      }).setNegativeButton(android.R.string.cancel, null).create();

        btnSaveSetting = (Button) v.findViewById(R.id.btn_save_settings);
        btnSaveSetting.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                saveSettings();
            }
        });
        return v;
    }

    //保存设置
    public void saveSettings()
    {
        SettingCenter.setFontSize(getActivity(), seekBarSetFontSize.getProgress()+seekBarSetFontSizeMin);
        Toast.makeText(getActivity(), "喵:首选项保存成功!!", Toast.LENGTH_SHORT).show();
    }
}
